//
//  WQTabBar.swift
//  WeiBo
//
//  Created by 张德华 on 7/23/16.
//  Copyright © 2016 ZhangDeHua. All rights reserved.
//

import UIKit

class WQTabBar: UITabBar {

    /**composeButton闭包*/
    var closure: (() -> ())?
 
    
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        backgroundColor = UIColor.whiteColor()
        
        addSubview(composButton)
        
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    // MARK: - ************重新布局tabBar按钮,添加中间按钮
    override func layoutSubviews() {
        
        let width = self.bounds.size.width * 0.2
        var index:CGFloat = 0
        
        for value in subviews {
            
            if value.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                
                value.frame = CGRect(x:  width*index, y: 0, width: width, height: self.bounds.height)
                
                index++
                if index == 2 {
                    index++
                }
                
                composButton.center.x = self.bounds.width * 0.5
                composButton.center.y = self.bounds.height * 0.5
            }
        }
    }
    
    
    // MARK: - ************composeButton按钮监听
    func composeButtonClicked() {
        
        closure!()
        
        
    }
    
    
    
    
    // MARK: - ************懒加载
    ///------ 撰写按钮
    private lazy var composButton: UIButton = {
       
        let composeButton = UIButton()
        
        composeButton.setImage(UIImage.init(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        composeButton.setImage(UIImage.init(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        composeButton.setBackgroundImage(UIImage.init(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        composeButton.setBackgroundImage(UIImage.init(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        composeButton.addTarget(self, action: #selector(WQTabBar.composeButtonClicked), forControlEvents: UIControlEvents.TouchUpInside)
        
        
        composeButton.sizeToFit()
        
        return composeButton
        
    }()
}
